<?php

	define('UPLOAD_OK','0');
	define('ERROR_SIZE','-1');
	define('ERROR_NAME','-2');
	define('ERROR_TYPE','-3');
	define('UPLOAD_ERROR','-4');
	define('TOO_MANY_FILES','-5');
	define('WRONG_FILE_TYPE','-6');

	class CUpload
	{
		static function Handle($szName,$iMaxSize = '',&$szUrlLocation = '',&$szServerLocation = '')
		{
			$szRandStr     = md5(uniqid(rand(),time()));
			$szWhereToMove = UPLOAD_DIR.'/'.$szRandStr;
			$szUploadUrl   = UPLOAD_URL.'/'.$szRandStr;
			
			if(isset($_FILES[$szName]) == false)
			{
				return UPLOAD_ERROR;
			}
			
			//preventing file upload attack	
			if($_FILES[$szName]['error'] != UPLOAD_ERR_OK )
			{
				return UPLOAD_ERROR;
			}
			
			//just in case..
			if($_FILES[$szName]['size'] <= 0)
			{
				return UPLOAD_ERROR;
			}
			
			//oversized file
			if($iMaxSize > 0 && $_FILES[$szName]['size'] > $iMaxSize)
			{
				return ERROR_SIZE;
			}	
			
			if(!is_uploaded_file($_FILES[$szName]['tmp_name']) ) 
			{
				return UPLOAD_ERROR;
			}
			
			
			move_uploaded_file($_FILES[$szName]['tmp_name'],$szWhereToMove);
			chmod($szWhereToMove,0666);
			
			$szUrlLocation     = $szUploadUrl;
			$szServerLocation  = $szWhereToMove;
			
			
			return UPLOAD_OK;
		}
		
	    static function HandleOneFromMore($szName, $iMaxSize = '', &$mMimeTypes = array(), &$szUrlLocation = '', &$szServerLocation = '', &$iWhichFile)
		{
          $iFilesCount = count($_FILES[$szName]['name']);
          $iFileAmount = 0;
          $iUploadingFile = -1;
          
          for($i=0; $i<$iFilesCount; $i++) {
            if (!empty($_FILES[$szName]['name'][$i])) {
              $iUploadingFile = $i;
              $iFileAmount++;
            }
          }

          if ($iFileAmount > 1) return TOO_MANY_FILES;
          
          $iWhichFile = $iUploadingFile;
          
          if ($iUploadingFile >= 0) {
            $szRandStr   = md5(uniqid(rand(),time()));
            $szWhereToMove = UPLOAD_DIR.'/'.$szRandStr;
            $szUploadUrl   = UPLOAD_URL.'/'.$szRandStr;
          
            if(isset($_FILES[$szName]) == false)
            {
              return UPLOAD_ERROR;
		    }
			
            //preventing file upload attack	
		    if($_FILES[$szName]['error'][$iUploadingFile] != UPLOAD_ERR_OK )
		    {
              return UPLOAD_ERROR;
		    }
		    
		    if( !empty($mMimeTypes) && !in_array($_FILES[$szName]['type'][$iUploadingFile], $mMimeTypes) ) {
              return WRONG_FILE_TYPE;
		    }
		    $mMimeTypes = $_FILES[$szName]['type'][$iUploadingFile];
		    
            //just in case..
		    if($_FILES[$szName]['size'][$iUploadingFile] <= 0)
		    {
		      return UPLOAD_ERROR;
		    }
			
 		    //oversized file
		    if($iMaxSize > 0 && $_FILES[$szName]['size'][$iUploadingFile] > $iMaxSize)
		    {
		      return ERROR_SIZE;
		    }	
		  
		    if(!is_uploaded_file($_FILES[$szName]['tmp_name'][$iUploadingFile]) ) 
		    {
		      return UPLOAD_ERROR;
		    }
		    
		    move_uploaded_file($_FILES[$szName]['tmp_name'][$iUploadingFile],$szWhereToMove);
		    chmod($szWhereToMove,0666);
		  
		    $szUrlLocation    = $szUploadUrl;
            $szServerLocation = $szWhereToMove;
		  }
		  return UPLOAD_OK;
		}
		
		//do ladowania wielu plikow na raz
		//trzeba dolozyc komunikaty o tym, ze wszystkie sie udaly
		//i o tym, ze np. tylko czesc sie udalo uploadowac
	    static function MultiplesHandle($szName,$iMaxSize = '',&$aUrlLocation = '',&$aServerLocation = '')
		{
          $iFilesCount = count($_FILES[$szName]['name']);
          
          for($i=0; $i<$iFilesCount; $i++) {
            $szRandStr   = md5(uniqid(rand(),time()));
            $szWhereToMove = UPLOAD_DIR.'/'.$szRandStr;
            $szUploadUrl   = UPLOAD_URL.'/'.$szRandStr;
			
			if(isset($_FILES[$szName]) == false)
			{
              return UPLOAD_ERROR;
			}
			
			//preventing file upload attack	
			if($_FILES[$szName]['error'][$i] != UPLOAD_ERR_OK )
			{
              return UPLOAD_ERROR;
			}
			
			//just in case..
			if($_FILES[$szName]['size'][$i] <= 0)
			{
			  return UPLOAD_ERROR;
			}
			
			//oversized file
			if($iMaxSize > 0 && $_FILES[$szName]['size'][$i] > $iMaxSize)
			{
			  return ERROR_SIZE;
			}	
			
			if(!is_uploaded_file($_FILES[$szName]['tmp_name'][$i]) ) 
			{
			  return UPLOAD_ERROR;
			}
			
			move_uploaded_file($_FILES[$szName]['tmp_name'][$i],$szWhereToMove);
			chmod($szWhereToMove,0666);
			
			$aUrlLocation[]    = $szUploadUrl;
			$aServerLocation[] = $szWhereToMove;
		  }
		  
		  return UPLOAD_OK;
		}
		
	}

?>